package com.kc.business.service.impl;

import com.kc.business.domain.KcUserWalletWithdrawApply;
import com.kc.business.domain.vo.KcUserWalletWithdrawApplyVo;
import com.kc.business.mapper.KcUserWalletWithdrawApplyMapper;
import com.kc.business.service.KcUserWalletWithdrawApplyService;
import com.kc.common.constant.BizConstants;
import com.kc.common.constant.RocketMqTagConstants;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.utils.RocketMQTemplate;
import com.kc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * FileName:   KcUserWalletWithdrawApplyServiceImpl
 * Author:     TP
 * Date:       12/21/20 10:01 AM
 * Description:用户钱包提现申请实现类
 */
@Service
public class KcUserWalletWithdrawApplyServiceImpl implements KcUserWalletWithdrawApplyService {

    @Autowired
    private KcUserWalletWithdrawApplyMapper walletWithdrawApplyMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public List<KcUserWalletWithdrawApplyVo> getList(KcUserWalletWithdrawApplyVo applyVo) {
        return walletWithdrawApplyMapper.getList(applyVo);
    }

    @Override
    public AjaxResult audit(KcUserWalletWithdrawApplyVo applyVo) {
        // 1.必填参数校验
        if (applyVo == null) {
            return AjaxResult.error("参数错误");
        }
        if (applyVo.getId() == null || applyVo.getId() < 0) {
            return AjaxResult.error("请指定要审核的申请记录");
        }
        if (!BizConstants.AUDIT_PASS.equals(applyVo.getStatus()) && !BizConstants.AUDIT_REJECT.equals(applyVo.getStatus())) {
            return AjaxResult.error("请指定要审核结果");
        }
        if (BizConstants.AUDIT_REJECT.equals(applyVo.getStatus())) {
            if (StringUtils.isEmpty(applyVo.getRejectReason())) {
                return AjaxResult.error("请说明拒绝原因");
            }
        }
        synchronized (this) {
            // 2.审核校验
            KcUserWalletWithdrawApply apply = walletWithdrawApplyMapper.selectByPrimaryKey(applyVo.getId());
            if (apply == null) {
                return AjaxResult.error("查无此项申请记录");
            }
            if (!BizConstants.AUDIT_ING.equals(apply.getStatus())) {
                return AjaxResult.error("申请记录已非审核中，请刷新重试");
            }
            // 3.审核操作
            KcUserWalletWithdrawApply toUpdateApply = new KcUserWalletWithdrawApply();
            toUpdateApply.setId(apply.getId());
            toUpdateApply.setStatus(applyVo.getStatus());
            toUpdateApply.setUpdateTime(applyVo.getUpdateTime());
            toUpdateApply.setAuditUserName(applyVo.getAuditUserName());
            toUpdateApply.setAuditUserId(applyVo.getAuditUserId());
            if (BizConstants.AUDIT_REJECT.equals(applyVo.getStatus())) {
                toUpdateApply.setRejectReason(applyVo.getRejectReason());
            }
            if (walletWithdrawApplyMapper.updateByPrimaryKeySelective(toUpdateApply) > 0) {
                if (BizConstants.AUDIT_PASS.equals(applyVo.getStatus())) {
                    rocketMQTemplate.sendCommonMessage(RocketMqTagConstants.USER_WALLET_WITHDRAW_AUDIT_PASS, apply.getId());
                } else {
                    rocketMQTemplate.sendCommonMessage(RocketMqTagConstants.USER_WALLET_WITHDRAW_AUDIT_REJECT, apply.getId());
                }
            }
            return AjaxResult.success();
        }
    }
}
